Abstract type

This article discusses types with no direct instances; see also Abstract data type.

In programming languages, an abstract type is a type in a nominative type system which cannot be instantiated. (However, it may have concrete subtypes that do have instances.) An abstract type may have no implementation, or an incomplete implementation. It may include abstract methods or abstract properties[1] that are shared by its subtypes.

A type that is not abstract is called a concrete type.

In many object oriented programming languages, abstract types are known as abstract base classes. In some languages, abstract types with no implementation are known as interfaces. Other names for languages features that are (or may be) used to implement abstract types include traits, mixins, flavors, or roles.

Contents

Signifying abstract types

Abstract classes can be created, signified, or simulated in several ways:

Example (Java)

abstract class Demo {
    // An abstract class may include abstract methods, which have no implementation.
    abstract public int sum(int x, int y);
 
    // An abstract class may also include concrete methods.
    public int product(int x, int y) { return x*y; }
}
 
interface DemoInterface {
    // All methods in an interface are abstract.
    int getLength();
}

Use of abstract types

Abstract types are an important feature in statically typed OO languages. Many dynamically typed languages have no equivalent feature (although the use of duck typing makes abstract types unnecessary); however traits are found in some modern dynamically-typed languages.

Some authors argue that classes should be leaf classes (have no subtypes), or else be abstract.[2][3]

Abstract types are useful in that they can be used to define and enforce a protocol; a set of operations which all objects that implement the protocol must support.

References

[4]

  1. ^ http://download.oracle.com/javase/tutorial/java/IandI/abstract.html
  2. ^ Riel, Arthur (1996). Object-Oriented Design Heuristics. Addison-Wesley Professional. p. 89. ISBN 0-201-63385-X. 
  3. ^ Meyers, Scott (1996). More Effective C++. Addison-Wesley Professional. p. 258. ISBN 020163371x. 
  4. ^ [www.headfirstlabs.com/books/hfjava/ Head First Java]. O'Reilly Media. 2003. pp. 688. ISBN 0-596-00920-8. www.headfirstlabs.com/books/hfjava/. 

External links